const Joi = require('joi');

class CloudValidate {

  async insert(param) {
    try {
      const schema = Joi.object({
        name: Joi.string()
          .required()
          .pattern(/^[^\s]+$/)
          .messages({
            'string.empty': '名称不能为空',
            'any.required': '名称是必填项',
            'string.pattern': '名称中不能包含特殊字符',
          }),

        host: Joi.string()
          .required()
          .pattern(/^[^\s]+$/)
          .messages({
            'string.empty': '地址不能为空',
            'any.required': '地址是必填项',
            'string.pattern': '地址中不能包含特殊字符',
          }),

        port: Joi.string()
          .required()
          .pattern(/^[^\s]+$/)
          .messages({
            'string.empty': '端口不能为空',
            'any.required': '端口是必填项',
            'string.pattern': '端口中不能包含特殊字符',
          }),

        path: Joi.string()
          .pattern(/^[^\s]+$/)
          .messages({
            'string.pattern': '路径中不能包含特殊字符',
          }),

        info: Joi.object(),

        type: Joi.string()
          .required()
          .messages({
            'string.empty': '端口不能为空',
            'any.required': '端口是必填项',
          }),
      });


      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }

  async list(param) {
    try {
      const schema = Joi.object({
        host: Joi.string(),
        port: Joi.string(),
        name: Joi.string(),
        path: Joi.string(),
        type: Joi.string(),
        method: Joi.string().default('desc'),
        order: Joi.string().default('create_time'),
        pageSize: Joi.string().default('10'),
        pageIndex: Joi.string().default('1'),
      });

      const value = schema.validateAsync(param);
      return value;
    } catch (error) {
      throw error;
    }
  }


  async info(param) {
    try {
      const schema = Joi.object({
        uuid: Joi.string(),
      });

      const value = schema.validateAsync(param);
      return value;
    } catch (error) {
      throw error;
    }
  }

  async update(param) {
    try {
      const schema = Joi.object({
        uuid: Joi.string()
          .required()
          .messages({
            'string.empty': 'uuid不能为空',
            'any.required': 'uuid是必填项',
          }),
        name: Joi.string(),
        host: Joi.string(),
        port: Joi.string(),
        path: Joi.string(),
        info: Joi.object(),
        type: Joi.string(),
      });

      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }

  async deleteMany(params) {
    try {
      const schema = Joi.object({
        ids: Joi.array(),
      });

      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  async setDefault(param) {
    try {
      const schema = Joi.object({
        uuid: Joi.string()
          .required()
          .pattern(/^[^\s]+$/)
          .messages({
            'string.empty': 'uuid不能为空',
            'any.required': 'uuid是必填项',
            'string.pattern': 'uuid中不能包含特殊字符',
          }),
      });

      const value = await schema.validateAsync(param);
      return value;
    } catch (error) {
      throw error;
    }
  }
}

module.exports = new CloudValidate();
